package com.hskn.hss.module.alarmperson.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.core.utils.poi.ExcelUtil;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.alarmashbin.entity.AlarmAshbin;
import com.hskn.hss.module.alarmperson.entity.AlarmPerson;
import com.hskn.hss.module.alarmperson.entity.AlarmPersonCharts;
import com.hskn.hss.module.alarmperson.mapper.AlarmPersonMapper;
import com.hskn.hss.module.alarmperson.service.IAlarmPersonService;
import com.hskn.hss.module.alarmperson.vo.AlarmPersonTableDetailVO;
import com.hskn.hss.module.alarmperson.vo.AlarmPersonVO;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.tlequip.mapper.TlEquipMapper;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Api(tags = {"-tl_alarm_person"}, description = "人员报警")
@Slf4j
@RestController
@RequestMapping("/hss/alarmperson")
public class AlarmPersonController {

    @Autowired
    private IAlarmPersonService alarmPersonService;

    @Resource
    private AlarmPersonMapper alarmPersonMapper;

    @Autowired
    ISysDepartService departService;

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = alarmPersonService.mList(entity, params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "当天人员异常接口", notes = "当天人员异常")
    @RequestMapping(value = "abnormal", method = RequestMethod.POST)
    public AjaxResult<Object> abnormal(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = alarmPersonService.getAbnormal();
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "alarm_person", idKey = "alarmPersonIds", bean = AlarmPerson.class, beanMapper = AlarmPersonMapper.class, names = "depName, personName")
    @ApiOperation(value = "批量处理接口", notes = "批量处理")
    @RequestMapping(value = "deal", method = RequestMethod.POST)
    public AjaxResult<Object> deal(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            alarmPersonService.deal(params);
            return AjaxResult.success("处理成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "selectAlarmPersonForCharts", method = RequestMethod.POST)
    public AjaxResult<Object> selectAlarmPersonForCharts(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            AlarmPerson alarmPerson=new AlarmPerson();
            if(params!=null){
                alarmPerson=JSONObject.parseObject(params.toJSONString(),AlarmPerson.class);
            }
            if(StringUtils.isNotEmpty(alarmPerson.getDepId())){
                List<String> depIds=departService.getChildIdsByIId(alarmPerson.getDepId());
                alarmPerson.setDepIds(depIds);
            }
            return AjaxResult.success( alarmPersonService.selectAlarmPersonForCharts(alarmPerson));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "selectAlarmPersonForChartsTable", method = RequestMethod.POST)
    public AjaxResult selectAlarmPersonForChartsTable(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            AlarmPerson alarmPerson=new AlarmPerson();
            if(params!=null){
                alarmPerson=JSONObject.parseObject(params.toJSONString(),AlarmPerson.class);
            }
            if(StringUtils.isNotEmpty(alarmPerson.getDepId())){
                List<String> depIds=departService.getChildIdsByIId(alarmPerson.getDepId());
                alarmPerson.setDepIds(depIds);
            }
            if(params!=null&&params.containsKey("pageNum")&&params.containsKey("pageSize")){
                startPage(params.getInteger("pageNum"),params.getInteger("pageSize"));
            }else{
                return AjaxResult.fail("pageNum，pageSize不能为空");
            }

            return AjaxResult.success( Tools.getDataTable(alarmPersonService.selectAlarmPersonForChartsTable(alarmPerson)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "alarmPersonChartsTableExcelExport", method = RequestMethod.POST)
    public com.hskn.hss.core.domain.AjaxResult alarmPersonChartsTableExcelExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            AlarmPerson alarmPerson=new AlarmPerson();
            if(params!=null){
                alarmPerson=JSONObject.parseObject(params.toJSONString(),AlarmPerson.class);
            }
            if(alarmPerson.getPersonIds() == null || alarmPerson.getPersonIds().size()>0){
            List<AlarmPersonCharts> alarmPersonChartsList=alarmPersonService.selectAlarmPersonForChartsTable(alarmPerson);
            //工具类实例化
            ExcelUtil<AlarmPersonCharts> util = new ExcelUtil<AlarmPersonCharts>(AlarmPersonCharts.class);
            //数据载体转换
            return util.exportExcel(alarmPersonChartsList,"alarmPersonChartsTableExcelExport");
            }else{
                return com.hskn.hss.core.domain.AjaxResult.error("请选择至少一条数据导出");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return com.hskn.hss.core.domain.AjaxResult.error("操作失败");
        }
    }

    @PostMapping("excelExport")
    public com.hskn.hss.core.domain.AjaxResult excelExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            QueryWrapper<AlarmPerson> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().orderByDesc(AlarmPerson::getCreateTime);
            List<String> ids= Arrays.asList(params.getString("ids").split(","));
            if(StringUtils.isNotEmpty(ids) && !(ids.size() ==1 && StringUtils.isEmpty(String.valueOf(ids.get(0))))){
                queryWrapper.lambda().in(AlarmPerson::getAlarmPersonId,ids);
            }
            String alarmPersonId = params.getString("alarmPersonId");
            if (StringUtils.isNotEmpty(alarmPersonId)) {
                queryWrapper.lambda().eq(AlarmPerson::getAlarmPersonId, alarmPersonId);
            }
            String personId = params.getString("personId");
            if (StringUtils.isNotEmpty(personId)) {
                queryWrapper.lambda().eq(AlarmPerson::getPersonId, personId);
            }
            String alarmTypeId = params.getString("alarmTypeId");
            if (StringUtils.isNotEmpty(alarmTypeId)) {
                queryWrapper.lambda().eq(AlarmPerson::getAlarmTypeId, alarmTypeId);
            }
            String alarmTypeName = params.getString("alarmTypeName");
            if (StringUtils.isNotEmpty(alarmTypeName)) {
                queryWrapper.lambda().like(AlarmPerson::getAlarmTypeName, alarmTypeName);
            }
            String startDate = params.getString("startDate");
            String endDate = params.getString("endDate");
            if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
                queryWrapper.lambda().between(AlarmPerson::getAlarmTime, startDate + " 00:00:00", endDate + " 23:59:59");
            }
            List<AlarmPerson> alarmPeoples = alarmPersonMapper.selectList(queryWrapper);
            //工具类实例化
            ExcelUtil<AlarmPersonVO> util = new ExcelUtil<>(AlarmPersonVO.class);
            //数据载体转换
            List<AlarmPersonVO> alarmPersonVO=JSONObject.parseArray(JSONObject.toJSONString(alarmPeoples),AlarmPersonVO.class);
            //导出
            return util.exportExcel(alarmPersonVO,"人员报警");

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }
    }

    @PostMapping("alarmPersoneDetailExport")
    public com.hskn.hss.core.domain.AjaxResult alarmPersoneDetailExport(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            AlarmPerson alarmPerson=new AlarmPerson();
            if(params!=null){
                alarmPerson=JSONObject.parseObject(params.toJSONString(),AlarmPerson.class);
            }
            if(alarmPerson.getIds() == null || alarmPerson.getIds().size()>0){
            List<AlarmPersonTableDetailVO> alarmPersonTableDetailVOList = alarmPersonService.getAlarmPersonTableDetailVOS(alarmPerson);
            //工具类实例化
            ExcelUtil<AlarmPersonTableDetailVO> util = new ExcelUtil<>(AlarmPersonTableDetailVO.class);
            //导出
            return util.exportExcel(alarmPersonTableDetailVOList,"alarmPersoneDetailExport");
            }else{
                return com.hskn.hss.core.domain.AjaxResult.error("请选择至少一条数据导出");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }
    }

    /**
     * 报警统计表明细
     * @param request
     * @return
     */
    @RequestMapping(value = "selectAlarmPersonForTableDetail", method = RequestMethod.POST)
    public AjaxResult<Object> selectAlarmPersonForTableDetail(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            AlarmPerson alarmPerson=new AlarmPerson();
            if(params!=null){
                alarmPerson=JSONObject.parseObject(params.toJSONString(),AlarmPerson.class);
            }
            return AjaxResult.success(alarmPersonService.selectAlarmPersonForTableDetail(alarmPerson));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
}

